#ifndef FRMSERIALPORT_H
#define FRMSERIALPORT_H

#include <QWidget>
#include <QSerialPort>
#include <QSerialPortInfo>
#include <QTimer>
#include "frmmain.h"
#include "config.h"

namespace Ui {
class frmserialport;
}

class frmmain;

class frmserialport : public QWidget
{
    Q_OBJECT

public:
    explicit frmserialport(QWidget *parent = nullptr);
    ~frmserialport();

private slots:
    void on_connectButton_clicked();
    void on_enterConfigModeButton_clicked();
    void on_exitConfigModeButton_clicked();
    void on_sendBaseStationButton_clicked();
    void on_sendMobileStationButton_clicked();
    void on_sendConfigButton_clicked();
    void on_cancelButton_clicked();
    void on_saveConfigButton_clicked();
    void on_loadConfigButton_clicked();
    void updateSendConfigButtonState();
    void onSerialDataReceived(const QString &data);
    void onResponseTimeout();

private:
    Ui::frmserialport *ui;
    frmmain *mainWindow;
    bool isConfigMode;
    bool isWaitingForResponse;
    QString lastCommand;
    QTimer responseTimer;
    int currentQueryTab;
    bool isWaitingForTcpConfig;

    void populateSerialPorts();
    void sendConfigModeCommand(bool enter);
    void updateMainResponse(const QString &msg);
    void enableConfigControls(bool enable);
    void resetConfigFields();
    void parseDeviceResponse(const QString &data);
    void startResponseTimer();
    void updateNmeaConfig(const QString &data);
    void updateRtcmConfig(const QString &data);
    void updateBaseStationConfig(const QString &data);
    void updateGyroConfig(const QString &data);
    void updateAccelConfig(const QString &data);
    void enableTcpConfigControls(bool enable);
    void resetTcpConfigFields();
    void updateTcpConfig(const QString &data);
};

#endif // FRMSERIALPORT_H